package com.maizhi.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.maizhi.dao.impl.Page;
import com.maizhi.hr.model.*;
import com.maizhi.httpmodel.Json;
import com.maizhi.httpmodel.xjInfo;
import com.maizhi.service.FilesServiceI;
import com.maizhi.service.hrServiceI;
import com.maizhi.util.StringUtil;
import com.maizhi.model.Files;


@Controller
@RequestMapping("/hrController")
public class hrController extends BaseController {
	
	Logger logger = Logger.getLogger(hrController.class);
	
	hrServiceI hrService;
	FilesServiceI fileService;

	public hrServiceI getHrService() {
		return hrService;
	}

	@Autowired
	public void setHrService(hrServiceI hrService) {
		this.hrService = hrService;
	}
	
	public FilesServiceI getFileService() {
		return fileService;
	}

	@Autowired
	public void setFileService(FilesServiceI fileService) {
		this.fileService = fileService;
	}
	
	@RequestMapping(params = "lwImpl")
	@ResponseBody
	public Json lwImpl(String saveFileId){
		logger.info("导入文件： "+saveFileId);
		Json json = new Json();
		try{
			if(saveFileId!=null && !saveFileId.equals("")){
				Files files = (Files) fileService.get(Files.class, saveFileId);
				logger.info("files : "+files.getFileName());
				if(files!=null){
					String subfix = files.getFileSubffix().toUpperCase(); 
					logger.info("后缀： "+subfix);
					if(subfix.equals(".XLS") || subfix.equals(".XLSX")){
						int n = hrService.lwImpl(files);
						json.setSuccess(true);
						json.setMsg("导入完成");
					}
				}
			}
		}catch(Exception e){
			json.setMsg("导入数据异常:"+e.getMessage());
			e.printStackTrace();
		}
		return json;
	}
	
	@RequestMapping(params = "lwClear")
	@ResponseBody
	public Json lwClear(){
		Json json = new Json();
		try{
			if(hrService.lwClear()){
				json.setSuccess(true);
				json.setMsg("劳务工授权奖金已清空");
			}else{
				json.setMsg("劳务工授权奖金清空失败");
			}
		}catch(Exception e){
			json.setMsg("劳务工授权奖金清空异常:"+e.getMessage());
			e.printStackTrace();
		}
		return json;
	}
	
	
	@RequestMapping(params = "wbImpl")
	@ResponseBody
	public Json wbImpl(@RequestParam String saveFileId){
		logger.info("导入文件： "+saveFileId);
		Json json = new Json();
		try{
			if(saveFileId!=null && !saveFileId.equals("")){
				Files files = (Files) fileService.get(Files.class, saveFileId);
				logger.info("files : "+files.getFileName());
				if(files!=null){
					String subfix = files.getFileSubffix().toUpperCase(); 
					logger.info("后缀： "+subfix);
					if(subfix.equals(".XLS") || subfix.equals(".XLSX")){
						int n = hrService.wbImpl(files);
						json.setSuccess(true);
						json.setMsg("导入完成");
					}
				}
			}
		}catch(Exception e){
			json.setMsg("导入数据异常:"+e.getMessage());
			e.printStackTrace();
		}
		return json;
	}
	
	@RequestMapping(params = "wbClear")
	@ResponseBody
	public Json wbClear(){
		Json json = new Json();
		try{
			if(hrService.wbClear()){
				json.setSuccess(true);
				json.setMsg("外包工授权奖金已清空");
			}else{
				json.setMsg("外包工授权奖金清空失败");
			}
		}catch(Exception e){
			json.setMsg("外包工授权奖金清空异常:"+e.getMessage());
			e.printStackTrace();
		}
		return json;
	}
	
	
	
	@RequestMapping(params = "xjImpl")
	@ResponseBody
	public Json xjImpl(String saveFileId){
		logger.info("导入文件： "+saveFileId);
		Json json = new Json();
		try{
			if(saveFileId!=null && !saveFileId.equals("")){
				Files files = (Files) fileService.get(Files.class, saveFileId);
				logger.info("files : "+files.getFileName());
				if(files!=null){
					String subfix = files.getFileSubffix().toUpperCase(); 
					logger.info("后缀： "+subfix);
					if(subfix.equals(".XLS") || subfix.equals(".XLSX")){
						int n = hrService.xjImpl(files);
						json.setSuccess(true);
						json.setMsg("导入完成");
					}
				}
			}
		}catch(Exception e){
			json.setMsg("导入数据异常:"+e.getMessage());
			e.printStackTrace();
		}
		return json;
	}
	
	@RequestMapping(params = "xjClear")
	@ResponseBody
	public Json xjClear(){
		Json json = new Json();
		try{
			if(hrService.xjClear()){
				json.setSuccess(true);
				json.setMsg("休假信息已清空");
			}else{
				json.setMsg("休假信息清空失败");
			}
		}catch(Exception e){
			json.setMsg("休假信息清空异常:"+e.getMessage());
			e.printStackTrace();
		}
		return json;
	}
	
	@RequestMapping(params = "lwList")
	public ModelAndView lwList(HrLwImp lw,Page page){
		 ModelAndView model = new ModelAndView("/hr/hr_lwList");
		 String hql="FROM HrLwImp t where 1=1";
		 if(!StringUtil.nullTrim(lw.getEmployeeId()).equals("")){
			 hql+=" and t.employeeId='"+lw.getEmployeeId()+"'";
		 }
		 if(!StringUtil.nullTrim(lw.getEmployeeName()).equals("")){
			 hql+=" and t.employeeName like '%"+lw.getEmployeeName()+"%'";
		 }
		 page.setCount(hrService.getHqlCounts(hql));
		 List<HrLwImp> list = hrService.findByHqlAndPage(hql+"  ORDER BY t.createDate desc", page);
		 model.addObject("list",list);
		 return model;
	}
	
	@RequestMapping(params = "wbList")
	public ModelAndView wbList(HrWbImp lw,Page page){
		 ModelAndView model = new ModelAndView("/hr/hr_wbList");
		 String hql="FROM HrWbImp t where 1=1 ";
		 if(!StringUtil.nullTrim(lw.getEmployeeId()).equals("")){
			 hql+=" and t.employeeId='"+lw.getEmployeeId()+"'";
		 }
		 if(!StringUtil.nullTrim(lw.getEmployeeName()).equals("")){
			 hql+=" and t.employeeName like '%"+lw.getEmployeeName()+"%'";
		 }
		 page.setCount(hrService.getHqlCounts(hql));
		 List<HrLwImp> list = hrService.findByHqlAndPage(hql+"  ORDER BY t.createDate desc", page);
		 model.addObject("list",list);
		 return model;
	}
	
	@RequestMapping(params = "xjList")
	public ModelAndView xjList(HrXjInfo lw,Page page){
		 ModelAndView model = new ModelAndView("/hr/hr_xjList2");
		 String hql="FROM HrXjInfo t where 1=1";
		 if(!StringUtil.nullTrim(lw.getEmployeeId()).equals("")){
			 hql+=" and t.employeeId='"+lw.getEmployeeId()+"'";
		 }
		 if(!StringUtil.nullTrim(lw.getEmployeeName()).equals("")){
			 hql+=" and t.employeeName like '%"+lw.getEmployeeName()+"%'";
		 }
		 page.setCount(hrService.getHqlCounts(hql));
		 List<HrLwImp> list = hrService.findByHqlAndPage(hql+"  ORDER BY t.createDate desc", page);
		 model.addObject("list",list);
		 return model;
	}
	
	@RequestMapping(params = "xjSqlList")
	public ModelAndView xjSqlList(HrXjInfo lw,Page page){
		 ModelAndView model = new ModelAndView("/hr/hr_xjList");
		 String sql= " SELECT                                  "+
					 "     t.*,                                "+
					 "     b.ext_all_cnt,                      "+
					 "     c.ext_cnt                           "+
					 " FROM                                    "+
					 "     (select * from hr_xj_info where 1=1 ";
					 if(!StringUtil.nullTrim(lw.getEmployeeId()).equals("")){
						 sql+=" and employee_id='"+lw.getEmployeeId()+"'";
					 }
					 if(!StringUtil.nullTrim(lw.getEmployeeName()).equals("")){
						 sql+=" and employee_name like '%"+lw.getEmployeeName()+"%'";
					 }
				sql+=") t ";
					 
				sql+="     LEFT JOIN (SELECT                   "+
					 "         xj_id,                          "+
					 "         COUNT(xj_id) AS ext_all_cnt     "+
					 "     FROM                                "+
					 "         hr_xj_info_ext                  "+
					 "     GROUP BY                            "+
					 "         xj_id) b ON t.id=b.xj_id        "+
					 "     LEFT JOIN (SELECT                   "+
					 "         xj_id,                          "+
					 "         COUNT(xj_id) AS ext_cnt         "+
					 "     FROM                                "+
					 "         hr_xj_info_ext                  "+
					 "     WHERE                               "+
					 "         STATUS=0                        "+
					 "     GROUP BY                            "+
					 "         xj_id) c ON t.id=c.xj_id        ";
		 
		 page.setCount(hrService.getSqlCounts(sql));
		 List<Map> list = hrService.findBySqlAndPageMap(sql+"  ORDER BY t.create_date desc", page);
		 model.addObject("list",list);
		 return model;
	}
	
	
	@RequestMapping(params = "xjInfo")
	public ModelAndView xjInfo(@RequestParam String Id,@RequestParam String employeeId){
		 ModelAndView model = new ModelAndView("/hr/hr_xjInfo");
		 HrXjInfo xj = (HrXjInfo)hrService.get(HrXjInfo.class,Id);
		 HrUserInfo user = (HrUserInfo)hrService.get(HrUserInfo.class, employeeId);
		 HrXjInfoExt xjExt = (HrXjInfoExt)hrService.get(HrXjInfoExt.class,Id);
		 model.addObject("xj",xj);
		 model.addObject("user",user);
		 model.addObject("xjExt",xjExt);
		 return model;
	}
	
	@RequestMapping(params = "xjInfoExt")
	public ModelAndView xjInfoExt(@RequestParam String xjId,@RequestParam String param,@RequestParam String employeeId){
		 ModelAndView model = new ModelAndView("/hr/hr_xjInfo_ext");
		 String hql = "FROM HrXjInfoExt t where t.xjId='"+xjId+"'";
		 if(!StringUtil.nullTrim(param).equals("")){
			 if(param.equals("currMonth")){
				 hql+=" and date_format(t.createDate,'%Y%m')=date_format(now(),'%Y%m')";
			 }else if(param.equals("lastMonth")){
				 hql+=" and PERIOD_DIFF( DATE_FORMAT(NOW(),'%Y%m' ),DATE_FORMAT(create_date,'%Y%m'))=1";
			 }else if(param.equals("currYear")){
				 hql+=" and date_format(t.createDate,'%Y')=date_format(now(),'%Y')";
			 } 
		 }
		 hql+=" order by t.createDate desc";
		 
		 List<HrXjInfoExt> extList = hrService.find(hql);
		 model.addObject("xjId",xjId);
		 model.addObject("xjExt",extList);
		 return model;
	}
	
	@RequestMapping(params = "xjEdit")
	@ResponseBody
	public Json xjEdit(@RequestParam String id,HrXjInfoExt xjExt){
		 Json json = new Json();
		 try{
//			 HrXjInfo info = (HrXjInfo)hrService.get(HrXjInfo.class,id);
//			 info.setAllCnt(xj.getAllCnt());
//			 info.setUseCnt(xj.getUseCnt());
//			 info.setRestCnt(xj.getRestCnt());
//			 info.setRestRemark(xj.getRestRemark());
//			 info.setRemark(xj.getRemark());
			 HrXjInfoExt ext = new HrXjInfoExt();
			 BeanUtils.copyProperties(xjExt,ext);
			 ext.setId(StringUtil.uuid());
			 ext.setXjId(id);
			 ext.setStatus(0);
			 ext.setCreateDate(new Date());
			 hrService.save(ext);
			 json.setSuccess(true);
			 json.setMsg("保存完成");
		 }catch(Exception e){
			 json.setMsg("保存异常: "+e.getMessage());
			 e.printStackTrace();
		 }
		 return json;
	}
	
	@RequestMapping(params = "lwInfo")
	public ModelAndView lwInfo(@RequestParam String Id,@RequestParam String employeeId){
		 ModelAndView model = new ModelAndView("/hr/hr_lwInfo");
		 HrLwImp xj = (HrLwImp)hrService.get(HrLwImp.class,Id);
		 HrUserInfo user = (HrUserInfo)hrService.get(HrUserInfo.class, employeeId);
		 model.addObject("xj",xj);
		 model.addObject("user",user);
		 return model;
	}
	
	@RequestMapping(params = "lwEdit")
	@ResponseBody
	public Json lwEdit(HrLwImp lw){
		 Json json = new Json();
		 try{
			 HrLwImp info = (HrLwImp)hrService.get(HrLwImp.class,lw.getId());
			 info.setIsJj(lw.getIsJj());
			 info.setXbReward(lw.getXbReward());
			 info.setZjReward(lw.getZjReward());
			 info.setHjReward(lw.getHjReward());
			 info.setRemark(lw.getRemark());
			 hrService.save(info);
			 json.setSuccess(true);
			 json.setMsg("保存完成");
		 }catch(Exception e){
			 json.setMsg("保存异常: "+e.getMessage());
			 e.printStackTrace();
		 }
		 return json;
	}
	
	@RequestMapping(params = "wbInfo")
	public ModelAndView wbInfo(@RequestParam String Id,@RequestParam String employeeId){
		 ModelAndView model = new ModelAndView("/hr/hr_wbInfo");
		 HrWbImp xj = (HrWbImp)hrService.get(HrWbImp.class,Id);
		 HrUserInfo user = (HrUserInfo)hrService.get(HrUserInfo.class, employeeId);
		 model.addObject("xj",xj);
		 model.addObject("user",user);
		 return model;
	}
	
	@RequestMapping(params = "wbEdit")
	@ResponseBody
	public Json wbEdit(HrWbImp wb){
		 Json json = new Json();
		 try{
			 HrWbImp info = (HrWbImp)hrService.get(HrWbImp.class,wb.getId());
			 info.setKhScore(wb.getKhScore());
			 info.setKhLevel(wb.getKhLevel());
			 info.setKhPer(wb.getKhPer());
			 info.setRemark(wb.getRemark());
			 hrService.save(info);
			 json.setSuccess(true);
			 json.setMsg("保存完成");
		 }catch(Exception e){
			 json.setMsg("保存异常: "+e.getMessage());
			 e.printStackTrace();
		 }
		 return json;
	}
	

}
